<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17/12/26
 * Time: 11:28
 */

class UserModel
{

    public function signInAction($content,$ip)
    {
        $uDetail =  Db::name('users_detail')
            ->field('id,firm,fid')
            ->where(['phone'=>$content['phone']])
            ->find();
        if($uDetail['fid']){
            $where = ['id'=>$uDetail['fid']];
        }elseif($uDetail['firm']){
            $where = ['code'=>$uDetail['firm']];
        }else{
            $where = ['code'=>$content['firm']];
        }
        $firm = Db::name('firm')
            ->field('id,title,code,content,image')
            ->where($where)
            ->find();
        if($uDetail){
            if($firm['code'] == $content['firm']){
                if(empty($uDetail['firm']) || empty($uDetail['fid'])){
                    $updata['firm'] = $content['firm'];
                    $updata['fid'] = $firm['id'];
                }
                $updata['lastip'] =$ip;
                Db::name('users_detail')->where(['phone'=>$content['phone'],'status'=>1])->update($updata);
            }else{
                $data['code'] = 400;
                $data['message'] = '您所对应的企业码错误';
                return $data;
            }
            $udid = $uDetail['id'];
        }else{
            $nickname = '新用户'.mb_substr($content['phone'],7,4);
            $user['phone'] = $content['phone'];
            $user['avatar'] ='https://img.livestaring.com/people.png';
            $user['nickname'] = $nickname;
            $user['firm'] = $content['firm'];
            $user['fid'] = $firm['id'];
            $user['sex'] = 0;
            $user['status'] = 1;
            $user['type'] = 0;
            $user['section'] = '未填写';
            $user['duties'] = '未填写';
            $user['createip'] = $ip;
            $user['lastip'] = $ip;
            $udid = Db::name('users_detail')->insertGetId($user);
        }
        $data['data'] = $this->infoAction($udid);
        unset($firm['id']);
        $data['share'] = $firm;
        return $data;
    }

    public function infoAction($udid)
    {
        return Db::name('users_detail')
            ->field('id,phone,avatar,nickname,firm,fid,sex,status,type,section,duties')
            ->where(['id'=>$udid])
            ->find();
    }

    public function rankingAction($data,$udid)
    {
        $redis = Yaf_Registry::get('redis');
        $lists = json_decode($redis->hget('Ranking',$data['firm']),true);
        if(empty($lists)){
            $lists = Db::name('ranking')
                ->field('sum(total) as total,sum(correct) as correct,count(llid) as number,udid')
                ->where(['fid'=>$data['fid']])
                ->group('udid')
                ->order('correct desc , correct/total desc')
                ->select();
            $redis->hset('Ranking',$data['firm'], json_encode($lists));
        }
        $data['count'] = Db::name('question')->where(['firm'=>$data['firm']])->count();
        unset($data);
        foreach ($lists as $k=>$v) {
            if($v['udid'] == $udid){
                $data['ranking'] = '第 '. ($k + 1) .' 名';
                break;
            }
        }
        if(empty($data['ranking'])){
            $data['ranking'] = '暂无排名';
        }

        return $data;
    }

    public function editAction($data,$udid,$path,$file)
    {
        if($path && $file){
            $avatar = Db::name('users_detail')->field('avatar')->where('id',$udid)->find();
            $imgUrl = parse_url($avatar['avatar']);
            $hostUrl = parse_url(HTTP_WEB);
            $model = new ModelModel();
            if(!empty($imgUrl['host']) && !empty($hostUrl['host']) && ($imgUrl['host'] == $hostUrl['host'])){
                $model->deleteImg($imgUrl['path']);
            }
            $data['avatar'] = $model->moveImg($path,$file,1);
        }
        Db::name('users_detail')->where('id',$udid)->update($data);
        $msg['data'] =  $this->infoAction($udid);
        $msg['data']['ranking'] = $this->rankingAction($msg['data'],$udid);
        $msg['message'] = '资料修改成功';
        return $msg;
    }

    public function recordAction($udid,$page,$number=PAGE_NUMBER)
    {
        $data['lists'] = Db::name('order')
            ->field('o.number,FORMAT(o.money /100,2) as money,o.lasttime,o.type,o.status,c.name')->alias('o')
            ->join('config c','c.id = o.cid')
            ->where('udid',$udid)
            ->page($page,$number)
            ->order('o.id desc')
            ->select();
        $data['page'] = $page;
        $data['count'] = Db::name('order')->where('udid',$udid)->count();
        return $data;
    }

    public function liveAction($user,$page,$number=PAGE_NUMBER)
    {
        $data['lists'] = Db::name('live_list')
            ->field('
            ll.id as llid,
            ll.name,
            ifnull(ll.password,"0") as password,
            ll.viewing,
            ll.status,
            left(ifnull(v.play_time, "0000-00-00 00:00:00"),10) as play_time,
            left(ifnull(v.play, "00:00:00"),5) as play,
            ud.nickname,
            ifnull(b.image,"") as bimage,
            ifnull(v.image,"") as limage
            ')->alias('ll')
            ->join('users_detail ud','ll.udid = ud.id')
            ->join('backdrop b','ll.bid = b.id','LEFT')
            ->join('video v','ll.vid = v.id','LEFT')
            ->where(['ll.fid'=>$user['fid'],'ll.udid'=>$user['id'],'ll.status'=>['in','7,8,9']])
            ->page($page,$number)
            ->order('llid desc')
            ->select();
        $data['page'] = $page;
        $data['count'] = Db::name('live_list')->where(['fid'=>$user['fid'],'udid'=>$user['id'],'status'=>['in','7,8,9']])->count();
        return $data;
    }


    public function meetingAction($user,$page,$number=PAGE_NUMBER)
    {
        $data['lists'] = Db::name('meeting')
            ->field('
            m.id as mid,
            m.name,
            ifnull(m.password,"0") as password,
            m.viewing,
            m.status,
            left(ifnull(v.play_time, "0000-00-00 00:00:00"),10) as play_time,
            left(ifnull(v.play, "00:00:00"),5) as play,
            ud.nickname,
            ifnull(b.image,"") as bimage,
            ifnull(v.image,"") as limage
            ')->alias('m')
            ->join('users_detail ud','m.udid = ud.id')
            ->join('backdrop b','m.bid = b.id','LEFT')
            ->join('video v','m.vid = v.id','LEFT')
            ->where(['m.fid'=>$user['fid'],'m.udid'=>$user['id'],'m.status'=>['in','7,8,9']])
            ->page($page,$number)
            ->order('mid desc')
            ->select();
        $data['page'] = $page;
        $data['count'] = Db::name('meeting')->where(['fid'=>$user['fid'],'udid'=>$user['id'],'status'=>['in','7,8,9']])->count();
        return $data;
    }

    public function delMeetingAction($user,$mid)
    {
        $where['id'] =$mid;
        $where['fid'] =$user['fid'];
        $where['udid'] =$user['id'];
        $where['status'] =9;
        $del = Db::name('meeting')->where($where)->update(['status'=>0]);
        if($del){
            $msg['message'] = '会议删除成功';
        }else{
            $msg['message'] = '回放生成中不可删除';
            $msg['code'] = 400;
        }
        return $msg;
    }

    public function delLiveAction($user,$llid)
    {
        $where['id'] =$llid;
        $where['fid'] =$user['fid'];
        $where['status'] =9;
        $where['udid'] =$user['id'];
        $del = Db::name('live_list')->where($where)->update(['status'=>0]);
        if($del){
            $msg['message'] = '课程删除成功';
        }else{
            $msg['message'] = '回放生成中不可删除';
            $msg['code'] = 400;
        }
        return$msg;
    }
}